import { getTagData } from "../../../../utils/util"

// pages/groups/room/setting/setting.js
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    tapSettingZK: function (params) {
        if (!/[0-9A-F]{4}/.test(this.data.zkFormData.zkDeviceCode)) {
            wx.showToast({
              title: '设备码范围0-9A-F',
              icon: 'none'
            })
            return;
        }
        if (!/[0-9]{6}/.test(this.data.zkFormData.zkLinkPwd)) {
            wx.showToast({
              title: '联机密码6位数字',
              icon: 'none'
            })
            return;
        }
        wx.$http.put({url: '/szm/room', data: {...this.data.zkFormData, id: this.roomId}}).then(res => {
            wx.showToast({
              title: "配置成功",
            });
            this.init();
       }).catch(err => {
       })
    },
    tapDeleteZK: function (params) {
        wx.$http.put({url: '/szm/room', data: {zkLinkPwd: '', zkDeviceCode: '', id: this.roomId}}).then(res => {
            wx.showToast({
              title: "删除成功",
            });
            this.init();
       }).catch(err => {
       })
    },
    tapSettingED: function (params) {
        if (!/[0-9]{6}/.test(this.data.edFormData.linkPwd)) {
            wx.showToast({
              title: '联机密码6位数字',
              icon: 'none'
            })
            return;
        }
        const {placeId} = wx.getStorageSync('place');
        wx.$http.put({url: '/szm/device', data: {...this.data.edFormData, placeId, roomId: this.roomId}}).then(res => {
            wx.showToast({
                title: "配置成功",
            });
            this.init();
        }).catch(err => {
        })
    },
    tapDeleteED: function (params) {
        if (this.data.edFormData.id) {
            wx.$http.put({url: '/szm/device', data: {linkPwd: '', id: this.data.edFormData.id, roomId: 0, placeId: 0}}).then(res => {
                wx.showToast({
                  title: "删除成功",
                });
                this.init();
           }).catch(err => {
           })
        } else {
            wx.showToast({
                title: "未配置E盾",
                icon: 'none'
            });
        }
    },

    bindZKInput: function(e) {
        let {field} = getTagData(e)
        field = field.replace(/_(\w)/g, (all, $1) => $1.toUpperCase())
        this.setData({[`zkFormData.${field}`]: e.detail.value})
    },

    bindEDInput: function(e) {
        let {field} = getTagData(e)
        field = field.replace(/_(\w)/g, (all, $1) => $1.toUpperCase())
        this.setData({[`edFormData.${field}`]: e.detail.value})
    },

    tapZKEyes: function() {
        this.setData({showZKPassword: !this.data.showZKPassword});
    },
    tapEDEyes: function() {
        this.setData({showEDPassword: !this.data.showEDPassword});
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const {id, rname} = options;
        this.roomId = id;
        wx.setNavigationBarTitle({
          title: rname,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getDeviceAction();
        this.init();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    tapEDAction: function (params) {
      this.setData({showActionsheet: !0})  
    },

    init() {
        wx.$http.get({url: `/szm/room/setting/${this.roomId}`}).then(res => {
            const {deviceName, linkPwd, zkLinkPwd, zkDeviceCode, deviceId} = res.data;
            const edFormData = {
                id: deviceId,
                linkPwd
            }
            const zkFormData = {
                zkLinkPwd,
                zkDeviceCode
            }
            const {deviceActions} = this.data;
            deviceActions.forEach(g => g.type = g.value == deviceId ? 'warn' : '')
            this.setData({
                deviceActions,
                zkFormData,
                edFormData,
                deviceName: deviceName || ""
            })
        });
    },

    async getDeviceAction() {
        const res = await wx.$http.get({url: '/szm/device/actionSheet'});
        this.setData({deviceActions: res.data});
    },

    /**action sheet 回调 */
    tapEDunItem: function(e){
        setTimeout(() => {
            this.setData({showActionsheet: !1})
        }, 300);
        const {value} = e.detail
        const {deviceActions} = this.data;
        const deviceName = deviceActions.find(d => d.value === value).text;
        deviceActions.forEach(g => g.type = g.value === value ? 'warn' : '')
        this.setData({
            ['edFormData.id']: value,
            deviceName,
            deviceActions
        });
    },
})